import { createWebHashHistory, createRouter, RouteRecordRaw } from 'vue-router'
import Layout from '../components/layout/index.vue'

const history = createWebHashHistory()

// 同步路由
export const constantRouterMap: RouteRecordRaw[] = [
  { path: '/404', name: 'error', component: () => import('../views/404/index.vue'), meta: { hidden: true } },
  {
    path: '/',
    component: Layout,
    redirect: 'homepage',
    children: [
      {
        path: 'homepage',
        component: () => import('../views/main/homepage/index.vue'),
        name: '首页',
        meta: {
          title: '首页',
          icon: 'home',
          fixed: true,
          roles: 'home'
        }
      }
    ]
  }
]

if (import.meta.env.MODE !== 'production' || (import.meta.env.MODE === 'production' && import.meta.env.VITE_APP_BIND_LOGIN_IN_PRODUCTION === 'false'))
  constantRouterMap.push({
    path: '/login',
    meta: { hidden: true },
    component: () => import('../views/login/index.vue')
  })

// 异步路由
export const asyncRouterMap: RouteRecordRaw[] = [
  {
    path: '/system',
    component: Layout,
    name: '系统管理',
    redirect: '/system/userset',
    meta: { title: '系统管理', icon: 'setting', roles: 'sysSet' },
    children: [
      {
        path: 'userset',
        name: '用户管理',
        component: () => import('../views/main/system/userset/index.vue'),
        meta: { title: '用户管理', roles: 'sysSetUser' }
      },
      {
        path: 'roleset',
        name: '角色管理',
        component: () => import('../views/main/system/roleset/index.vue'),
        meta: { title: '角色管理', roles: 'sysSetRoler' }
      },
      {
        path: 'dictionary',
        name: '系统字典',
        component: () => import('../views/main/system/dictinary/index.vue'),
        meta: { title: '系统字典', roles: 'sysSetDic' }
      },
      {
        path: 'systemlog',
        name: '系统日志',
        component: () => import('../views/main/system/systemlog/index.vue'),
        meta: { title: '系统日志', roles: 'sysSetData' }
      }
    ]
  }
]

// 生产环境不打包调试模块
if (import.meta.env.MODE !== 'production' && import.meta.env.VITE_APP_DEBUG === 'true')
  asyncRouterMap.push({
    path: '/admin',
    component: Layout,
    redirect: 'log',
    children: [
      {
        path: 'log',
        component: () => import('../views/dev-log/index.vue'),
        name: '调试',
        meta: {
          title: '调试',
          icon: 'terminal'
        }
      }
    ]
  })

asyncRouterMap.push({ path: '*', redirect: '/404', meta: { hidden: true } })

const router = createRouter({
  history,
  routes: constantRouterMap
})

declare module '@vue/runtime-core' {
  // eslint-disable-next-line no-unused-vars
  interface ComponentCustomProperties {
    $router: typeof router
  }
}

export default router
